package com.dbh.company.buildbook;

import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.dbh.company.buildbook.bean.BatchUpdateReq;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dongbanghui
 * @create 2023/11/13
 */
public class GifUrlFlush {

    private static final String cookie = "crosSdkDT2019DeviceId=-uudqok-p0z6tw-u5tozzmsej9s39l-t6zeqcw9i; ketest-env=production; _ga=GA1.2.2121015128.1686129466; wx_device_id=bUTx-Zs85VX-EJ38i6V8ZKBFvZtdlNd_7n7X1cq3OrE=; ke_uuid=f975736ecd03071f59f4d5c22f49dd01; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22ce301845-8fe2-4a00-96fb-8f5ac09fbf35%22%2C%22first_id%22%3A%2218976818cbd94a-066522c3f35ba78-7e565470-2073600-18976818cbe2427%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fcn.bing.com%2F%22%7D%2C%22%24device_id%22%3A%2218976818cbd94a-066522c3f35ba78-7e565470-2073600-18976818cbe2427%22%7D; _ga_F0DQ6TTSVW=GS1.2.1699427842.52.1.1699428872.60.0.0; login_ucid=1000000029138498; security_ticket_test=QNSzh+dO1n2l9ak6u8+452hFcsIuC3MrU39diVXDK2nSvZj60kYd4iF6SclLkyTwVa2itnu1ifpEw3HY2v1w7Bq/BVZ2aWcL7fuF01hXMr5MnCWgg61G7+t6T2OT+YZNT7ynbU3PtYCfvS0e86vjugt6sCW66eXg5xO4SDKlANM=; factory_token=2.0111a9beb284385a9f0001cb54f11d107e634a7629; lianjia_token=2.001550711b80c1953604f9cfa9475006e1; _account_=dongbanghui001; _userFullEmail_=dongbanghui001%40ke.com; pilot_ke_token=2.0111a9beb284385a9f0001cb54f11d107e634a7629; casgwusercred=JeB0aqScXfnBqHHKMTc-uCdFr2IbNcMqZSfISwPAi19gAKp4VDxESZPQHvYfMVc7tuCI48ayeaftZevFgu3gZ04uh3rZcx1D5NqRCOYVIRlftDhMS8QbSmbO5lGdqHqGKdqgq_oOsanduYJ-aG66vH9kCbmzeTy-JvsX7Ks2j6Keh5f8EU2t2v7PxyRNL0rs; crosgwusercred=P9_5unP3MN3GId8ENxx3Rcs8QFrkjGXhNmQjvfw1LjAp7N6dMLCRgInCEJafxlIVkDFzi9RgsS_THObVXstJkW2nPayUy6LrlEYBxXAaDcw6bbad3f82daf4185eac56f4301ddf8bf; mr_backend_service_uid=1000000029138498; security_ticket=bQUH+MMG1szyKss57i/5PXKSogKrIsmEWfXYVcELoktHg23eb6Bd3iWYGvYHzdaUKHD41Q11ZjKfUm4NAjLN97qiHlNBKe6nh8Nh4qx7rMBvF6pFUkeOlE6ZyaR303p/kW9rsz6hDu027FOzLTFfGhbtfsafpIvddddomI4y4iY=; ketest-session=aqppjmmb4aztnmfsmqhgvq7yjbduawip; ketest-user=eyJsb2dpbl91c2VyIjogIjEwMDAwMDAwMjkxMzg0OTgiLCAiYWNjb3VudCI6ICJkb25nYmFuZ2h1aTAwMSIsICJhY2NvdW50U3lzdGVtSWQiOiAiZW1wbG95ZWUiLCAiYXZhdGFyIjogIm51bGwiLCAiYmlydGhkYXkiOiAibnVsbCIsICJidXNpbmVzc1Rva2VuIjogIjIuMDExMWE5YmViMjg0Mzg1YTlmMDAwMWNiNTRhODc1MTE5Y2Y2MjQ4NTZhIiwgImNvcnBJZCI6ICJudWxsIiwgImRpc3BsYXlOYW1lIjogIlx1ODQ2M1x1NWUyZVx1OGY4OSIsICJlbWFpbCI6ICJkb25nYmFuZ2h1aTAwMUBrZS5jb20iLCAiZ2VuZGVyIjogIk1BTEUiLCAiaWQiOiAiMTAwMDAwMDAyOTEzODQ5OCIsICJtZmFBdXRoTWV0aG9kTGV2ZWwiOiAiMCIsICJwYXNzZWRNZmFSZWFzb24iOiAiaXBXaGl0ZUxpc3QiLCAicGhvbmUiOiAiMTg1MDE5OTI1OTQiLCAicmVhbE5hbWUiOiAiXHU4NDYzXHU1ZTJlXHU4Zjg5IiwgInNlcnZpY2VJZCI6ICJkYXl1LmtlLmNvbSIsICJzdGF0ZSI6ICJOT1JNQUwiLCAidGd0SWQiOiAiVEdULTE3NzQ1MjktQklLczFON1JkVk1vOWZHODlELXBSZW05SWd5ckZZOG0tMFlBNUVhVlpHLWQ5RVgxTjE3V3RtWXdOcHB5d1g4dkxxRS1rZS5jb20iLCAidWNpZCI6ICIxMDAwMDAwMDI5MTM4NDk4IiwgInVjbmFtZSI6ICJcdTg0NjNcdTVlMmVcdThmODkiLCAidWlkIjogIjEwMDAwMDAwMjkxMzg0OTgiLCAidXNlcl9jb2RlIjogIjI5MTM4NDk4IiwgInVzZXJUYWciOiAiMCIsICJ2ZXJzaW9uIjogIjg4MCIsICJ6aGlkYW9fdWNpZCI6ICIyMTEwMDAwMDA0MDk2ODA5IiwgIm5leHRfdXJsIjogImh0dHA6Ly9rZXRlc3QtcHJvLmtlLmNvbS8jL3Rvb2wvZGF5dUNoYXRib3QifQ=a2V0ZXN0LXVzZXI==; lianjia_uuid=21d46b6a-e2dd-410b-a9f4-de2f7f9aa026; lianjia_ssid=c110fe00-3c87-43d2-90bb-26fda623222c; mr_backend_login_user=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzZXNzaW9uSWQiOiIyLjAwMTA4MzgyMDQ4NTEyNjYyOTAxMmEzY2I2ODM1ZTBmMzEifQ.9GmFuFLcrdk4Ynu6LfUxQK2w9Kry4g3Ew6nvY5vXGPU";
    public static void main(String[] args) {
        int startId = 140534;
        int endId = 145690;
        while (startId < endId) {
            int tempStartId = startId;
            startId += 100;
            int temp = startId;
            List<Long> noGifUrlProjectIdList = getNoGifUrlProjectIdList(tempStartId, temp);
            for (Long id : noGifUrlProjectIdList) {
                Console.log("执行任务：{}", id);
                batchUpdateSunlightGifImage(id.intValue());
//                genGifImage(id);
            }
        }
    }

    public static List<Long> getNoGifUrlProjectIdList(Integer startId, Integer endId) {
        String url = "http://pre-mr.ke.com/api/v1/getNoGifUrlProjectIdList";
        BatchUpdateReq batchUpdateReq = new BatchUpdateReq();
        batchUpdateReq.setStartId(startId);
        batchUpdateReq.setEndId(endId);
        List<Long> idList = new ArrayList<>();
        try {
            String body = HttpRequest
                    .post(url)
                    .body(JSONUtil.toJsonStr(batchUpdateReq))
                    .execute()
                    .body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            Object o = jsonObject.get("data");
            String dataJsonStr = JSONUtil.toJsonStr(o);
            idList = JSON.parseArray(dataJsonStr, Long.class);
        } catch (Exception e) {
            Console.log("startId:{}, endId:{}, 请求异常：{}", startId, endId, e.getMessage());
        }
        return idList;
    }

    public static void batchUpdateSunlightGifImage(Integer id) {
        String url = "http://backend-mr.ke.com/v1/projects/sunlight/batchUpdateSunlightGifImage";
        BatchUpdateReq batchUpdateReq = new BatchUpdateReq();
        batchUpdateReq.setStartId(id);
        batchUpdateReq.setEndId(id);
        String body = HttpRequest
                .post(url)
                .body(JSONUtil.toJsonStr(batchUpdateReq))
                .header("Cookie", cookie)
                        .execute()
                .body();
        Console.log(body);
    }

    public static void genGifImage(Long projectId) {
        String url = "http://backend-mr.ke.com/v1/projects/sunlight/genGifImage?projectId=" + projectId;
        String body = HttpRequest.get(url)
                .header("Cookie", cookie)
                .execute()
                .body();
        Console.log(body);
    }
}
